package com.app.Activity;

import it.unipr.ce.dsg.s2p.sip.Address;
import java.util.ArrayList;
import com.app.Controller.NetworkController;
import com.app.DTO.AccountDTO;
import com.app.Network.NetworkLayer;
import com.app.PhanQuyen.Quyen;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import app.com.ModuleRole.R;

public class frmMainActivity extends Activity {
	/** Called when the activity is first created. */
	public static Handler handler = new Handler();
	public static Runnable raShowMessage;
	public static Runnable raMoiVaoNhomNotif;
	
	public static Context baseContext;
	ArrayList<Quyen> rule;
	AccountDTO account = null;
	static String loi = "";
	int port = 8800;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// handler=new Handler();

		// nh.getRule().get(0).Send(,new Address("192.149.1.1"));
		Button button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				NetworkController.sendGroupInvite(new Address("192.168.1.77:" + port));

				Log.d("Phu", "Da send");
				// network.account.getRule().get(0).setSend(false);
			}
		});
		raShowMessage = new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				showMessage(loi);
			}
		};
		raMoiVaoNhomNotif = new Runnable() {

			@Override
			public void run() {
				displayNotification();
			}
		};
	}

	public void showMessage(String message) {
		Log.d("Phu", "toi day chua");
		AlertDialog alert = new AlertDialog.Builder(this).create();
		alert.setMessage(message);
		alert.setTitle("test");
		alert.show();
	}

	public static void show(String message) {
		loi = message;
		handler.post(raShowMessage);

	}

	@Override
	public void onStart() {
		super.onStart();
		if (NetworkController._peer==null) {

			account = new AccountDTO();

			rule = new ArrayList<Quyen>();
			
			account.setRule(rule);
			NetworkController._peer = new NetworkLayer(null, "asdsd", "MES", port, account);
			this.setTitle(NetworkController._peer.getAddressPeer());
			baseContext = getBaseContext();
		}
	}

	public void displayNotification() {
		Intent intent = new Intent(this, frmNotificationMoiVaoNhom.class);
		intent.putExtra("ID", 1);
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
				intent, 0);
		NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Notification notif = new Notification(R.drawable.ic_launcher,
				"Co thang moi vao nhom", System.currentTimeMillis());
		notif.setLatestEventInfo(this, "vao nhom ko ku?", "Vao de",
				pendingIntent);

		notiManager.notify(1, notif);

	}
}